package com.wang.head.filter;

import com.wang.common.pojo.ApiResponse;
import com.wang.common.pojo.ApiResponseEnum;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IUsersDbService;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Collections;


/**
 * 创建一个过滤器的步骤和Servlet一样 实现一个Filter接口
 *
 * @author wangshMac
 */
@WebFilter(urlPatterns = "/head/*")
public class AuthHeadFilter implements Filter {
    /*
     * 这两个对象,中的彼此引用 是null; 如果想赋值,必须调用set方法
     */
    private IUsersDbService usersDbService;

    @Override
    public void destroy() {
        ConstatFinalUtil.SYS_LOGGER.info("==AuthHeadFilter==destroy=");
    }

    /**
     * 过滤器所做的事情=====Servlet中的Service
     */
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /*
         * 虽然参数中指定的是ServletRequest,但是通过运行时打印发现: req就是HttpServletRequest;
         */
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        /*
         * 获取到Session session是httpServletRequest才有的方法
         */
        HttpSession session = request.getSession();
        /*
         * 取票 getAttribute之前,一定要确定setAttribute
         */
        AUsers users = (AUsers) session.getAttribute("users");
        // RequestFacade rs = null ;
        /*
         * 在该拦截的地方拦截了.(他发现了游客)
         *
         * ~请出示一下你的票, 有票:sorry,知道了,玩的开心;放行 木有票:请到大门处,补票;提供一个友好的页面
         *
         * ~在登陆提交操作,为用户发票(登陆成功)
         */
        if (users != null) {
            /* 放行--登陆成功 */
            this.authVerify(chain, request, response, users);
            return;
        }
        apiResponse.setCode(ApiResponseEnum.INFO_LOGIN_ILLEGAL.getStatus());
        apiResponse.setInfo(
                ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
                Collections.EMPTY_MAP);
        /* 存储信息 */
        session.setAttribute("response", apiResponse.toJSON());
        response.sendRedirect(request.getContextPath() + "/NoLoginServlet?method=login");
    }

    /**
     * 权限验证 ~肯定是已经登陆的;就可以查询到管理员, ~就可以查询到角色 ~就可以查询到角色所应的菜单
     * ~拿着当前访问的url和菜单中对应的url,比对,如果比对成,有权限,比对失败,木有权限
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void authVerify(FilterChain chain, HttpServletRequest request, HttpServletResponse response,
                            AUsers users)
            throws IOException, ServletException {
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        ConstatFinalUtil.SYS_LOGGER.info("==AuthBackFilter==init=");
    }

}
